package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.*;
import com.qf.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService{

    @Autowired
    private DtsGoodsMapper goodsMapper;

    @Autowired
    private DtsOrderGoodsMapper orderGoodsMapper;

    @Autowired
    private DtsGoodsProductMapper goodsProductMapper;

    @Autowired
    private DtsCommentMapper commentMapper;

    @Autowired
    private DtsGoodsAttributeMapper goodsAttributeMapper;

    @Autowired
    private DtsGoodsSpecificationMapper goodsSpecificationMapper;

    @Override
    public int countGoods() {

        QueryWrapper<DtsGoods> qw = new QueryWrapper<>();
        qw.eq("deleted",false);

        Integer count = goodsMapper.selectCount(qw);

        return count;
    }

//    @Override
//    public List<Map> goods() {
//
//        List<Map> rows = orderGoodsMapper.selectGoodsDayStatis();
//
//        return rows;
//    }

    @Override
    public void delete(DtsGoods goods) {

        Integer goodsId = goods.getId();
        if (goodsId != null) {

            DtsGoodsProduct goodsProduct = new DtsGoodsProduct();
            goodsProduct.setDeleted(true);
            QueryWrapper<DtsGoodsProduct> goodsProduct_qw = new QueryWrapper<>();
            goodsProduct_qw.eq("goods_id", goodsId);
            goodsProductMapper.update(goodsProduct,goodsProduct_qw);

            DtsComment comment = new DtsComment();
            comment.setDeleted(true);
            QueryWrapper<DtsComment> comment_qw = new QueryWrapper<>();
            comment_qw.eq("type", 0);
            comment_qw.eq("value_id", goodsId);
            commentMapper.update(comment,comment_qw);

            DtsGoodsSpecification goodsSpecification = new DtsGoodsSpecification();
            goodsSpecification.setDeleted(true);
            QueryWrapper<DtsGoodsSpecification> goodsSpecification_qw = new QueryWrapper<>();
            goodsSpecification_qw.eq("goods_id", goodsId);
            goodsSpecificationMapper.update(goodsSpecification,goodsSpecification_qw);

            DtsGoodsAttribute goodsAttribute = new DtsGoodsAttribute();
            goodsAttribute.setDeleted(true);
            QueryWrapper<DtsGoodsAttribute> goodsAttribute_qw = new QueryWrapper<>();
            goodsAttribute_qw.eq("goods_id", goodsId);
            goodsAttributeMapper.update(goodsAttribute,goodsAttribute_qw);

            goods.setDeleted(true);
            goodsMapper.updateById(goods);

        }

    }
}
